package dao.Impl;

import dao.BaseDao;
import dao.RainDao;
import entity.Rainquality;

import javax.xml.transform.Result;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RainDaoImpl extends BaseDao implements RainDao {
    Rainquality rainquality;
    @Override
    public List<Rainquality> getAll() {
        String sql="SELECT id,districtName,monitorTime,rain,monitoringStation,monitoringAddress  FROM rainquality";
        ResultSet rs=executeQuery(sql);
        List<Rainquality> allList=new ArrayList<>();
        try {
            while (rs.next()){
                rainquality=new Rainquality();
                rainquality.setId(rs.getInt("id"));
                rainquality.setDistrictName(rs.getString("districtName"));
                rainquality.setMonitorTime(rs.getString("monitorTime"));
                rainquality.setRain(rs.getInt("rain"));
                rainquality.setMonitoringStation(rs.getString("monitoringStation"));
                rainquality.setMonitoringAddress(rs.getString("monitoringAddress"));
                allList.add(rainquality);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,pstmt,rs);
        }
        return allList;
    }

    @Override
    public int add(String districtName, String monitorTime, int rain, String monitoringStation, String monitoringAddress) {
        String sql="INSERT into rainquality (districtName,monitorTime,rain,monitoringStation,monitoringAddress) VALUES(?,?,?,?,?)";
        Object parms[]={districtName,monitorTime,rain,monitoringStation,monitoringAddress};
        int hanghshu=executeUpdate(sql,parms);
        return hanghshu;
    }

    @Override
    public boolean del(String contentId) {
        boolean flag=false;
        String sql="delete from rainquality where id=?";
        Object parms[]={contentId};
        int hanghshu=executeUpdate(sql,parms);
        if (hanghshu>0){
            flag=true;
        }
        return flag;
    }
}
